package com.sinaapp.kltq.dinosaurweather;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.sinaapp.kltq.data.CityListItem;
import com.sinaapp.kltq.weathermanager.WeatherManager;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class CityManagement extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.activity_city_management);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar_city_management);

        ((ImageView) findViewById(R.id.titleBack)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                back();
            }
        });

        ((ImageView) findViewById(R.id.titleAdd)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(CityManagement.this, AddCityActivity.class);
                intent.putExtra("cityIndex", getIntent().getIntExtra("cityIndex", 0));
                intent.putExtra("pageType", getIntent().getCharExtra("pageType", 'r'));
                startActivity(intent);
                overridePendingTransition(R.anim.anim_none, R.anim.anim_none);
            }
        });
        // 显示城市列表
        ListView listView = (ListView) findViewById(R.id.cityListView);
        List<CityListItem> cityList = new WeatherManager(getApplicationContext()).getCityList();
        ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();
        for (CityListItem item : cityList) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("btnRemove", R.drawable.btn_remove);
            map.put("itemText", item.city);
            map.put("btnDrag", R.drawable.btn_drag);
            listItem.add(map);
        }
        SimpleAdapter listItemAdapter = new MySimpleAdapter(
                this, listItem, R.layout.city_list_item_layout
                , new String[]{"btnRemove", "itemText", "btnDrag"}
                , new int[]{R.id.btnRemove, R.id.itemText, R.id.btnDrag});
        listView.setAdapter(listItemAdapter);
        // TODO: 城市拖拽
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                System.out.println("DRAG " + position);
            }
        });
    }

    @Override
    public void onBackPressed() {
        back();
        super.onBackPressed();
    }

    private void back() {
        // 确定跳转目标
        Intent intent = getIntent();
        int targetCityIndex = intent.getIntExtra("cityIndex", 0);
        char targetPageType = intent.getCharExtra("pageType", 'r');
        WeatherManager wm = new WeatherManager(getApplicationContext());
        List<CityListItem> cityList = wm.getCityList();
        // 如果长度为0
        if (cityList.size() == 0) {
            intent = new Intent(CityManagement.this, EmptyActivity.class);
            startActivity(intent);
            overridePendingTransition(R.anim.anim_none, R.anim.anim_none);
            finish();
            return;
        }
        // 如果长度不足
        if (targetCityIndex >= cityList.size()) {
            targetCityIndex = cityList.size() - 1;
        }
        intent = new Intent(CityManagement.this, FlipWhiteActivity.class);
        intent.putExtra("cityIndex", targetCityIndex);
        intent.putExtra("pageType", targetPageType);
        startActivity(intent);
        overridePendingTransition(R.anim.anim_none, R.anim.anim_none);
        finish();
    }

}
